package com.baizhi.controller;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.util.IdUtil;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Banner;
import com.baizhi.service.BannerService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;


    @RequestMapping("/selectAllBanner")
    @ResponseBody
    @LogAnnotation(value = "查询全部轮播图",type = "select")
    public Map selectAllBanner(int page ,int limit){
        return bannerService.selectAllBanner(page,limit);
    }

    @RequestMapping("/deleteBanner")
    @ResponseBody
    @LogAnnotation(value = "删除轮播图",type = "delete")
    public Map  deleteBanner(int bannerId){
        return bannerService.deleteBanner(bannerId);
    }

    @RequestMapping("/insertBanner")
    @ResponseBody
    @LogAnnotation(value = "添加轮播图",type = "insert")
     public Map insertBanner(Banner banner , MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println(banner);
        System.out.println(file);
        String realPath=request.getServletContext().getRealPath("/img");//绝对路径
        System.out.println(realPath);
        //做重命名处理，防止文件同名被覆盖

        String newFileName=  new Date().getTime()+"_"+file.getOriginalFilename();
        banner.setBannerImageUrl("/img/"+newFileName);
        file.transferTo(new File(realPath+"/"+newFileName));
        return bannerService.insertBanner(banner);
     }

     @RequestMapping("/selectByBannerId")
     @ResponseBody
     @LogAnnotation(value = "根据id查询轮播图信息",type = "select")
     public Banner selectByBannerId(int bannerId){
        return bannerService.selectByBannerId(bannerId);
     }

     @RequestMapping("/updateBanner")
     @ResponseBody
     @LogAnnotation(value = "修改轮播图信息",type = "update")
     public Map updateBanner(Banner banner){
        return bannerService.updateBanner(banner);
     }


     @RequestMapping("/updateBanner1")
     @ResponseBody
     @LogAnnotation(value = "修改轮播图信息",type = "update")
     public Map updateBanner1(Banner banner){
        return bannerService.updateBanner1(banner);

     }


   /* @RequestMapping("/updateBannerStates")
    @ResponseBody
    @LogAnnotation(value = "修改轮播图状态",type = "update")
     public Map updateBannerStates(Banner banner){
        return bannerService.updateBannerStates(banner);
     }
*/
    //批量下载功能
    @RequestMapping("/download")
    @ResponseBody
    @LogAnnotation(value = "导出轮播图数据",type="download")
    public Map selectAlls(){
        return bannerService.selectAlls();
    }

    @RequestMapping("/updates")
    @ResponseBody
    @LogAnnotation(value = "轮播图隐藏or显示",type = "update")
   public Map updates(Banner banner){
        return bannerService.updates(banner);
   }
}
